﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BLL;
using Model.Auxiliar;

namespace HRP.Private.Funcionarios.Controlos
{
    public partial class Editar_Documentos : System.Web.UI.UserControl
    {
        void Page_PreRender(object sender, EventArgs e)
        {
            if (FuncionarioActual != null)
            {
                if (!Page.IsPostBack)
                {
                    NumFuncionario = FuncionarioActual.Dados.id.ToString();                
                }
                NumFuncionario = FuncionarioActual.Dados.id.ToString();
                carregarDocs();
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            Page.PreRender += Page_PreRender;
        }

        private string _NumFuncionario
        {
            get
            {
                if (ViewState["NumFuncionario"] != null)
                {
                    return (string)ViewState["NumFuncionario"];
                }
                return string.Empty;
            }
            set
            {
                ViewState["NumFuncionario"] = value;
            }
        }

        public string NumFuncionario
        {
            get
            {
                return _NumFuncionario;
            }
            set
            {
                _NumFuncionario = value;
            }
        }

        public Funcionario FuncionarioActual { get; set; }


        

        void carregarDocs()
        {
            int idfunc = 0;
            //Int32.TryParse(Request.QueryString["id"], out idfunc);
            idfunc = FuncionarioActual.Dados.id;
            documentosTable.DataSource = Documento_BLL.Instance.obterDocumentosFuncionario(idfunc);
            documentosTable.DataBind();

        }


        public void alterarEstadoDoc(Object sender, CommandEventArgs e)
        {
            int docID = 0;
            Int32.TryParse(e.CommandArgument.ToString(), out docID);
            if (docID != 0 && Session["idclient"] != null && Session["userid"] != null)
            {
                Documento_BLL.Instance.setActivo_InactivoDOC(docID);
            }
        }


        public void abrirDocumentoCom(Object sender, CommandEventArgs e)
        {
            string url = "";
            url = e.CommandArgument.ToString();
            DownloadFile(url);

        }

        static public void DownloadFile(string filePath)
        {
            try
            {
                HttpContext.Current.Response.ContentType =
                            "application/octet-stream";
                HttpContext.Current.Response.AddHeader("Content-Disposition",
                  "attachment; filename=" + System.IO.Path.GetFileName(filePath));
                HttpContext.Current.Response.Clear();
                HttpContext.Current.Response.WriteFile(filePath);
                HttpContext.Current.Response.End();
            }
            catch (Exception ex)
            {
                return;
              //  throw ex;
            }
        }

    }
}